package itsos.service.impl;

import itsos.model.*;
import itsos.service.IssueService;
import itsos.dao.*;

import java.sql.Timestamp;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import itsos.common.*;

public class IssueServiceImpl implements IssueService{
	
	private IssueDao issueDao;
	private IssueHistoryDao issueHistoryDao;
	
	public void setIssueHistoryDao(IssueHistoryDao issueHistoryDao) {
		this.issueHistoryDao = issueHistoryDao;
	}

	public void setIssueDao(IssueDao issueDao) {
		this.issueDao = issueDao;
	}

	public boolean insertIssue(HttpServletRequest request)
	{
		Issue issue = new Issue();
		
		issue.setRepositoryID(new Long(Long.parseLong(request.getParameter("repositoryID"))));
		issue.setIssueTitle(request.getParameter("issueTitle"));
		issue.setIssueContent(request.getParameter("issueContent"));
		issue.setIssueAttriCode(new Long(Long.parseLong(request.getParameter("issueAttriCode"))));
		issue.setInsertTime(new Timestamp(System.currentTimeMillis()));
		issue.setInsertUserID((Long)request.getSession().getAttribute("memberID"));
		issue.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		issue.setUpdateUserID((Long)request.getSession().getAttribute("memberID"));
				
		if(issue.getIssueAttriCode().equals(ConstCode.REPORT))
		{
			issue.setPublishGroupCode(ConstCode.GENEREL);
			issue.setStateCode(ConstCode.COMPLETE);
			issueDao.insertIssue(issue);
		}
		else
		{
			issue.setPublishGroupCode(ConstCode.ADMINISTRATOR);
			issue.setStateCode(ConstCode.REGISTERED);
			issueDao.insertIssue(issue);
		}
		
		
		return true;
		
	}
	
	public List IssueList(HttpServletRequest request)
	{
		return issueDao.getIssueList(new Long(Long.parseLong(request.getParameter("repositoryID"))));
	}
	
	public Issue viewProcIssue(HttpServletRequest request)
	{
		Issue issue = new Issue();
		issue.setIssueID(new Long(Long.parseLong(request.getParameter("issueID"))));
		
		issue = issueDao.getProcIssueByIssueID(issue.getIssueID());
		
		return issue;
	}
	
	public Issue viewReptIssue(HttpServletRequest request)
	{
		Issue issue = new Issue();
		issue.setIssueID(new Long(Long.parseLong(request.getParameter("issueID"))));
		
		issue = issueDao.getReptIssueByIssueID(issue.getIssueID());
		
		return issue;
	}
	
	public void updateIssue(HttpServletRequest request)
	{
		
		Issue issue = new Issue();
		
		issue.setRepositoryID(new Long(Long.parseLong(request.getParameter("repositoryID"))));
		issue.setIssueTitle(request.getParameter("issueTitle"));
		issue.setIssueContent(request.getParameter("issueContent"));
		issue.setIssueAttriCode(new Long(Long.parseLong(request.getParameter("issueAttriCode"))));
		issue.setProcessorID(new Long(Long.parseLong(request.getParameter("processorID"))));
		issue.setPublishGroupCode(new Long(Long.parseLong(request.getParameter("publishGroupCode"))));
		issue.setStateCode(new Long(Long.parseLong(request.getParameter("stateCode"))));
		issue.setUpdateTime(new Timestamp(System.currentTimeMillis()));
		issue.setUpdateUserID(new Long(Long.parseLong(request.getParameter("updateUserID"))));
		
		issueDao.updateIssue(issue);
				
	}

	public Issue viewIssueWithIssueComment(HttpServletRequest request) {

		Issue issue = new Issue();
		
		issue.setIssueID(new Long(Long.parseLong(request.getParameter("issueID"))));
		
		issue = issueDao.getViewIssueWithIssueComment(issue.getIssueID());
		
		return issue;
	}
	
	
	public List getRegIssue(HttpServletRequest request)
	{
		List list = issueDao.getRegIssue(new Long(Long.parseLong(request.getParameter("mainIssueID"))));
		
		return list;
	}

	public void updateAdmin(HttpServletRequest request) {
		
		 //Issue issue = new Issue();
		
		Issue issue = issueDao.getReptIssueByIssueID(new Long(Long.parseLong(request.getParameter("regIssue"))));
		
		issue.setProcessorContent(request.getParameter("processorContent"));
		issue.setInsertTime(new Timestamp(System.currentTimeMillis()));
		issue.setIssueID(issue.getProcessorID());
		issueHistoryDao.insertIssueHistory(issue);
		
		issue.setIssueID(new Long(Long.parseLong(request.getParameter("regIssue"))));
		issue.setProcessorID(new Long(Long.parseLong(request.getParameter("devMember"))));
		issue.setProcessorContent(request.getParameter("processorContent"));
		issue.setStateCode(ConstCode.DEVELOPER_PROCESSING);
		issue.setPublishGroupCode(new Long(Long.parseLong(request.getParameter("publishGroupCode"))));
		
		issueDao.updateAdmin(issue);
		
		
	}

	public void editProcessorContent(HttpServletRequest request) {
		
		Issue issue = new Issue();
		
		
		issue.setIssueID(new Long(Long.parseLong(request.getParameter("issueID"))));
		issue.setProcessorContent(request.getParameter("processorContent"));
		issueDao.editProcessorContent(issue);
		issue = issueDao.getProcIssueByIssueID(new Long(Long.parseLong(request.getParameter("issueID"))));
		
		issue.setInsertTime(new Timestamp(System.currentTimeMillis()));
		issue.setInsertUserID(issue.getProcessorID());
		
		issueHistoryDao.insertIssueHistory(issue);
	}
	
	
	public void issueProc(HttpServletRequest request)
	{
		Issue issue = new Issue();
		issue.setProcessorContent(request.getParameter("processorContent"));
		issue.setStateCode(ConstCode.ADMIN_PROCESSING);
		issue.setIssueID(new Long(Long.parseLong(request.getParameter("issueID"))));
		issueDao.issueProc(issue);
		issue = issueDao.getProcIssueByIssueID(issue.getIssueID());
		issueHistoryDao.insertIssueHistory(issue);
			
		
		
	}

	public List adminProc(HttpServletRequest request) {
		
		List list = issueDao.adminProc(new Long(Long.parseLong(request.getParameter("repositoryID"))));
		
		return list;
	}

	public List issueToProc(HttpServletRequest request) {
		
		Issue issue = issueDao.getReptIssueByIssueID(new Long(Long.parseLong(request.getParameter("issueID"))));
		
		issue.setProcessorID(new Long(Long.parseLong(request.getParameter("devMember"))));
		issue.setPublishGroupCode(new Long(Long.parseLong(request.getParameter("publishGroupCode"))));
		issue.setStateCode(new Long(Long.parseLong(request.getParameter("stateCode"))));
		issue.setProcessorContent(request.getParameter("processorContent"));
		issue.setInsertTime(new Timestamp(System.currentTimeMillis()));
		issueHistoryDao.insertIssueHistory(issue);
		
		issue.setIssueID(new Long(Long.parseLong(request.getParameter("issueID"))));
		issueDao.updateAdmin(issue);
		
		
		List list = issueDao.adminProc(issue.getRepositoryID());
		
		return list;
	}
	
	
	public List issueTracking(HttpServletRequest request)
	{
		List list = issueHistoryDao.getIssueHistoryByIssueID(new Long(Long.parseLong(request.getParameter("issueID"))));
		
		return list;
	}
		
}
